package me.jessyan.armscomponent.commonsdk.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebviewUtil {

    @SuppressLint("WrongConstant")
    public static void loadData(Activity activity, WebView webView, String htmlData) {

        WebSettings settings = webView.getSettings();

        settings.setTextSize(WebSettings.TextSize.NORMAL);

//        settings.setJavaScriptEnabled(true);
//
//        settings.setNeedInitialFocus(false);
//
//        settings.setSupportZoom(true);
//
//        settings.setLoadWithOverviewMode(true);//适应屏幕
//
//        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//
//        settings.setLoadsImagesAutomatically(true);//自动加载图片
//
//        settings.setCacheMode(WebSettings.LOAD_DEFAULT |WebSettings.LOAD_CACHE_ELSE_NETWORK);


        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.getSettings().setAppCacheEnabled(true);
        //设置 缓存模式
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        // 开启 DOM storage API 功能
        webView.getSettings().setDomStorageEnabled(true);


        webView.setWebViewClient(new WebViewClient() {
            //覆盖shouldOverrideUrlLoading 方法
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.loadDataWithBaseURL(null, htmlData, "text/html", "utf-8", null);

//        webView.loadData(htmlData, "text/html; charset=UTF-8", null);//这种写法可以正确解码

    }
}
